package com.imut.lagain.controller;

import com.imut.lagain.entity.User;
import com.imut.lagain.service.IUserService;
import com.imut.lagain.util.JwtUtil;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试认证控制器 - 用于功能验证，绕过真实微信API
 */
@RestController
@RequestMapping("/test-auth")
@RequiredArgsConstructor
@Validated
public class TestAuthController {
    private static final Logger log = LoggerFactory.getLogger(TestAuthController.class);

    private final IUserService userService;
    private final JwtUtil jwtUtil;

    /**
     * 测试微信登录 - 使用模拟数据
     * @param loginRequest 登录请求
     * @return 登录结果
     */
    @PostMapping("/wx-login")
    public ResponseEntity<Map<String, Object>> testWxLogin(@Valid @RequestBody TestWxLoginRequest loginRequest) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            log.info("测试微信登录 - 使用模拟数据");
            
            // 模拟微信openid（基于code生成一个唯一标识）
            String mockOpenid = "test_openid_" + loginRequest.getCode().hashCode();
            
            log.info("模拟openid: {}", mockOpenid);
            
            // 查找或创建用户
            User user = userService.getOrCreateUser(
                mockOpenid, 
                null, 
                loginRequest.getNickname(), 
                loginRequest.getAvatarUrl(), 
                null
            );
            
            // 生成JWT令牌
            String token = jwtUtil.generateToken(user.getId(), user.getOpenid());
            
            response.put("success", true);
            response.put("message", "测试登录成功");
            response.put("token", token);
            response.put("user", user);
            response.put("note", "这是测试接口，使用了模拟的微信数据");
            
            log.info("测试登录成功，用户ID: {}, Token: {}", user.getId(), token);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("测试微信登录失败", e);
            response.put("success", false);
            response.put("message", "测试登录失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }

    /**
     * 测试登录请求类
     */
    public static class TestWxLoginRequest {
        @NotBlank(message = "微信授权码不能为空")
        private String code;
        
        private String nickname = "测试用户";
        private String avatarUrl = "https://example.com/avatar.jpg";

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getNickname() {
            return nickname;
        }

        public void setNickname(String nickname) {
            this.nickname = nickname;
        }

        public String getAvatarUrl() {
            return avatarUrl;
        }

        public void setAvatarUrl(String avatarUrl) {
            this.avatarUrl = avatarUrl;
        }
    }
}